import { message } from "antd"
import {
  getStoreList,
  findOne,
  deleteStore,
  updateStore,
  SearchStoreName,
  getStoreFinancialDetails,
  getStoreListPage,
  getStoreFinancialDetailsPage,
  getStoreFinancialDetailsDate,
  stick

} from '../services/buyer';


export default {
  namespace: 'buyer',

  state: {
    storeListItem: [],
    findOneItem: {},
    storeListTotal: "",
    storeListDetails: [],
  },

  effects: {
    *getStoreList({ payload }, { call, put }) {
      const data = yield call(getStoreList);
      console.log('data', data)
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            storeListItem: data.result.list,
            storeListTotal: data.result.totalElements
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *findOne({ payload }, { call, put }) {
      const data = yield call(findOne, payload);
      console.log('data', data)
      if (data.status == 0) {
        yield put({
          type: 'updateState',
          payload: {
            findOneItem: data.result
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *deleteStore({ payload }, { call, put }) {
      const data = yield call(deleteStore, payload);
      console.log('data', data)
      if (data.status == 0) {
        yield put({
          type: 'getStoreList',
        })
      } else {
        message.error(`${data.message}`)
      }
    },


    *updateStore({ payload }, { call, put }) {
      const data = yield call(updateStore, payload);
      if (data.status == 0) {
        message.success("编辑成功!")
        yield put({ type: "getStoreList" })
      } else {
        message.error('编辑失败，请检查问题。')
      }
    },

    *SearchStoreName({ payload }, { call, put }) {
      const data = yield call(SearchStoreName, payload);
      if (data.status == 0) {
        yield put({
          type: "updateState",
          payload: {
            storeListItem: data.result.list,
            storeListTotal: data.result.totalElements
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getStoreFinancialDetails({ payload }, { call, put }) {
      const data = yield call(getStoreFinancialDetails, payload);
      if (data.status == 0) {
        yield put({
          type: "updateState",
          payload: {
            storeListDetails: data.result,
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getStoreListPage({ payload }, { call, put }) {
      const data = yield call(getStoreListPage, payload);
      if (data.status == 0) {
        yield put({
          type: "updateState",
          payload: {
            storeListItem: data.result.list,
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getStoreFinancialDetailsPage({ payload }, { call, put }) {
      const data = yield call(getStoreFinancialDetailsPage, payload);
      if (data.status == 0) {
        yield put({
          type: "updateState",
          payload: {
            storeListDetails: data.result,
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },

    *getStoreFinancialDetailsDate({ payload }, { call, put }) {
      const data = yield call(getStoreFinancialDetailsDate, payload);
      if (data.status == 0) {
        yield put({
          type: "updateState",
          payload: {
            storeListDetails: data.result,
          }
        })
      } else {
        message.error(`${data.message}`)
      }
    },
    *stick({ payload }, { call, put }) {
      console.log('payload', payload)
      const data = yield call(stick, payload)
      if (data.status == 0) {
        yield put({
          type: "getStoreList"
        })
      }
    }

  },

  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === "/buyer/list") {
          dispatch({ type: "getStoreList" });
        }
      });
    }
  },

};
